package fr.greta.centreFormation.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.greta.centreFormation.bean.Candidat;
import fr.greta.centreFormation.bean.Etat;
import fr.greta.centreFormation.dao.CandidatHome;
import fr.greta.centreFormation.dao.EtatHome;

/**
 * Servlet implementation class ModifCandidat
 */
public class ModifCandidat extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ModifCandidat() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// HttpSession session=request.getSession(true);

		int idPersonne = Integer.parseInt(request.getParameter("idPersonne"));

		String nom = request.getParameter("nom");
		String prenom = request.getParameter("prenom");
		String login = request.getParameter("login");
		String password = request.getParameter("password");
		String adresse = request.getParameter("adresse");
		int codePostal = Integer.parseInt(request.getParameter("codepostal"));
		String ville = request.getParameter("ville");
		String mail = request.getParameter("mail");
		String tel1 = request.getParameter("tel1");

		Candidat candidat = new Candidat();

		candidat.setIdPersonne(idPersonne);
		candidat.setNom(nom);
		candidat.setPrenom(prenom);

		// Récupération de la date de naissance
		String jour = request.getParameter("jourNaissance");
		String mois = request.getParameter("moisNaissance");
		String annee = request.getParameter("anneeNaissance");

		String dateString = annee + "-" + mois + "-" + jour;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		java.util.Date dateJava = new java.util.Date();

		try {
			dateJava = sdf.parse(dateString);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		java.sql.Date dateSQL = new java.sql.Date(dateJava.getTime());

		candidat.setDateNaissance(dateSQL);

		candidat.setLogin(login);
		candidat.setPassword(password);
		candidat.setMail(mail);
		candidat.setAdresse(adresse);
		candidat.setCodePostale(codePostal);
		candidat.setVille(ville);
		candidat.setNumTel1(tel1);
		candidat.setNumtel2(request.getParameter("tel2"));
		candidat.setCompetence(request.getParameter("competences"));
		candidat.setMotivation(request.getParameter("motivation"));
		candidat.setCv(request.getParameter("cv"));

		Etat etat = new Etat();
		etat = EtatHome.selectUnEtatByIdEtat(Integer.parseInt(request
				.getParameter("etat")));
		candidat.setEtat(etat);

		ArrayList<Etat> lesEtats = new ArrayList<Etat>();
		lesEtats = EtatHome.selectAllEtats();

		if (nom == "" || prenom == "" || login == "" || password == ""
				|| adresse == "" || codePostal < 1000 || ville == ""
				|| mail == "" || tel1 == "") {

			String erreur = "Certaines données obligatoires sont manquantes";

			request.setAttribute("leCandidat", candidat);
			request.setAttribute("erreur", erreur);
			request.setAttribute("lesEtats", lesEtats);

			request.getRequestDispatcher("modifFicheCandidat.jsp").forward(
					request, response);

		} else {

			CandidatHome.updateUnCandidat(candidat);

			request.getRequestDispatcher("GestionCandidat").forward(request,
					response);
		}
	}
}
